package friendShips;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



@Path("/addFriend/{myUserName}/{username}")

public class AddFriend {
@GET
@Produces("text/html")
public String getUser(@PathParam("myUserName") String myUserName ,@PathParam("username") String userName) {

	SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
	Session session = sessionFactory.openSession();
	
	FriendManager manager = new FriendManager(session);
	
	Query query = session.createQuery("from User user where user.email = :newEmail");
	query.setParameter("newEmail", userName);
	
	List <User> list = query.list();
	Iterator<User> it=list.iterator();
	
	if (list.size()==0)
	{
		return "BAD";
	}
	FriendShip user= new FriendShip(myUserName, userName);
	manager.saveUser(user);
	session.flush();
	
	return "GOOD";
	
}	
	
}
